﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using park.cn.linkit.epark;

namespace park.Led
{
    public partial class List : System.Web.UI.Page
    {
        ServiceList ws = new ServiceList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["m"] != null)
            {
                Response.ContentType = "application/json";
                string m = Request.Params["m"].ToLower();
                string r = "{}";
                switch (m)
                {
                    case "search":
                        r = Search();
                        break;
                    case "delete":
                        r = Delete();
                        break;
                    case"getinfo":
                        r = GetInfo();
                        break;
                    case "edit":
                        r = Edit();
                        break;
                    case "add":
                        r = Add();
                        break;
                    case "getledparentinfo":
                        r = GetLedParentInfo();
                        break;
                    case "getledlocation":
                        r = GetLedLocation();
                        break;
                    case "setled":
                        r = SetLedsParams();
                        break;
                    case "inserttext":
                        r = InsertLedsText();
                        break;
                }
                Response.Write(r);
                Response.End();
            }
            
        }

        string InsertLedsText()
        {
            string ids = fix("ids");
            string entity = fix("entity");
            bool r = true;
            if (r)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }

        string SetLedsParams()
        {
            string ids = fix("ids");
            string entity = fix("entity");
            bool r = true;
            if (r)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }

        string fix(string key)
        {
            if (Request.Params[key] != null)
            {
                try
                {
                    return Uri.UnescapeDataString(Request.Params[key]);
                }
                catch
                {
                    return Request.Params[key];
                }
            }
            return "";
        }

        string GetLedLocation()
        {
            string id = fix("id");
            string ret = "{\"lng\":114.34,\"lat\":32.45}";
            return ret;
        }
        

        string GetLedParentInfo()
        {
            string cid = fix("cid");
            string aid = fix("aid");
            string sid = fix("sid");
            string ret = @"{""data"":[{""id"":456,""name"":""街道口诱导设备1"",""lng"":114.21 ,""lat"":32.45}]}";
            ret = ws.GetLedParentInfo(cid, aid, sid);
            return ret;
        }

        string Search()
        {
            string cid = fix("cid");
            string aid = fix("aid");
            string sid = fix("sid");
            string code = fix("code");            

            string result = @"{
                                ""data"":[
                                    {
                                    ""id"":123 
                                    ,""code"":""x123"" 
                                    ,""name"":""街道口诱导屏1"" 
                                    ,""ledCode"":""x345"" 
                                    ,""parentName"":""街道口诱导设备1"" 
                                    ,""parentId"":1234 
                                    ,""enable"":1 
                                    ,""defaultText"":""欢迎光临"" 
                                    }
                                ]
                                }";

            result = ws.ListLed(cid, aid, sid, code);

            return result;         

        }

        string Delete()
        {
            string id = Request.Params["id"];
            bool r = ws.DeleteLed(id);
            if (r)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }

        string GetInfo()
        {
            string id = fix("id");
            string ret =  @"{
                                    ""id"":123 
                                    ,""code"":""x123"" 
                                    ,""name"":""街道口诱导屏1"" 
                                    ,""ledCode"":""x345"" 
                                    ,""parentName"":""街道口诱导设备1"" 
                                    ,""parentId"":72 
                                    ,""enable"":1 
                                    ,""defaultText"":""欢迎光临"" 
                                    }";
            ret = ws.GetLedInfo(id);
            return ret;
        }

        string Edit()
        {
            string id = fix("id");
            string name = fix("name");
            string parentId = fix("parentId");
            string enable = fix("enable");
            string installTime = fix("installTime");
            string defaultText = fix("defaultText");
            bool r = ws.EditLed(id, enable, parentId, defaultText, name);
            if (r)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }

        string Add()
        {
            string code = fix("code");
            string name = fix("name");
            string ledCode = fix("ledCode");
            string parentId = fix("parentId");
            string enable = fix("enable");
            string installTime = fix("installTime");
            string defaultText = fix("defaultText");
            bool r = ws.AddLed(code, name, ledCode, parentId, enable, installTime, defaultText);
            if (r)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }
    }
}